var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "parentClassifyId",
            rootPId: -1
        },
        key: {
            url:"nourl",
            name:"classifyValue"
        }
    }
};
var ztree;

var vm = new Vue({
    el:'#pmpapp',
    data:{
        showList: true,
        title: null,

        classify:{
            parentClassifyId:0,
            classifyValue:null,
            code:0
        }
    },
    methods: {

        getClassify: function(){
            //加载商品分类树
            $.get(baseURL + "sys/classify/select", function(r){
                ztree = $.fn.zTree.init($("#classifyTree"), setting, r.data.classifyList);
                var node = ztree.getNodeByParam("id", vm.classify.parentClassifyId);
                ztree.selectNode(node);

                vm.classify.parentName = node.name;
            })
        },

        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.classify = {parentName:null,parentClassifyId:0,classifyLevel:0,code:0};
            vm.getClassify();
        },

        update: function () {
            var id = getClassifyId();
            if(id == null){
                return ;
            }

            $.get(baseURL + "sys/classify/info/"+id, function(r){
                vm.showList = false;
                vm.title = "修改";
                vm.classify = r.data.classify;

                vm.getClassify();
            });
        },

        del: function () {
            var id = getClassifyId();
            if(id <= 0){
                return ;
            }

            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/classify/delete",
                    data: "id=" + id,
                    success: function(r){
                        if(r.code === 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },

        saveOrUpdate: function () {
            if(vm.validator()){
                return ;
            }

            var url = vm.classify.id == null ? "sys/classify/save" : "sys/classify/update";
            $.ajax({
                type: "POST",
                url:  baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.classify),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },

        classifyTree: function(){
            layer.open({
                type: 1,
                offset: '30px',
                skin: 'layui-layer-molv',
                title: "选择分类",
                area: ['300px', '300px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#classifyLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    //选择上级菜单
                    vm.classify.parentClassifyId = node[0].parentClassifyId;
                    vm.classify.parentName = node[0].classifyValue;
                    vm.classify.classifyLevel = node[0].classifyLevel;

                    layer.close(index);
                }
            });
        },

        reload: function () {
            vm.showList = true;
            Classify.table.refresh();
        },

        validator: function () {
            if(isBlank(vm.classify.classifyValue)){
                alert("分类名称不能为空！");
                return true;
            }
        }
    }
});


var Classify = {
    id: "classifyTable",
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Classify.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: '商品分类Id', field: 'id', visible: false, align: 'center', valign: 'middle', width: '120px'},
        {title: '分类名称', field: 'classifyValue', align: 'center', valign: 'middle', sortable: true, width: '220px'},
        {title: '分类编码', field: 'code', align: 'center', valign: 'middle', sortable: true, width: '100px'},
        {title: '创建人', field: 'createUserName', align: 'center', valign: 'middle', sortable: true, width: '140px'},
        {title: '创建时间', field: 'createTime', align: 'center', valign: 'middle', sortable: true, width: '160px'},
        {title: '更新人', field: 'updateUserName', align: 'center', valign: 'middle', sortable: true, width: '140px'},
        {title: '更新时间', field: 'updateTime', align: 'center', valign: 'middle', sortable: true, width: '160px'}]
    return columns;
};


function getClassifyId () {
    var selected = $('#classifyTable').bootstrapTreeTable('getSelections');
    if (selected.length == 0) {
        alert("请选择一条记录");
        return null;
    } else {
        return selected[0].id;
    }
}


$(function () {
    var colunms = Classify.initColumn();
    var table = new TreeTable(Classify.id, baseURL + "sys/classify/list", colunms);
    table.setExpandColumn(2);
    table.setIdField("id");
    table.setCodeField("id");
    table.setParentCodeField("parentClassifyId");
    table.setExpandAll(true);
    table.init();
    Classify.table = table;
});
